package com.bestcem.xm.qdes.config;


import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName: VisiableThreadPoolTaskExecutor
 * @Description: 方便打印消息
 * @Author: chenglong.yue
 * @Date: 2022/11/28 10:20
 */
@Slf4j
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {

    private void showThreadPoolInfo(String prefix) {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        log.info("{}, {},taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
                this.getThreadNamePrefix(),
                prefix,
                threadPoolExecutor.getTaskCount(),
                threadPoolExecutor.getCompletedTaskCount(),
                threadPoolExecutor.getActiveCount(),
                threadPoolExecutor.getQueue().size()
        );
    }


    @Override
    public void execute(@NotNull Runnable task) {
        showThreadPoolInfo("1. do execute");
        super.execute(task);
    }


    @Override
    public void execute(@NotNull Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute");
        super.execute(task, startTimeout);
    }


    @NotNull
    @Override
    public Future<?> submit(@NotNull Runnable task) {
        showThreadPoolInfo("1. do submit");
        return super.submit(task);
    }


    @NotNull
    @Override
    public <T> Future<T> submit(@NotNull Callable<T> task) {
        showThreadPoolInfo("2. do submit");
        return super.submit(task);
    }

    @NotNull
    @Override
    public ListenableFuture<?> submitListenable(@NotNull Runnable task) {
        showThreadPoolInfo("1. do submitListenable");
        return super.submitListenable(task);
    }

    @NotNull
    @Override
    public <T> ListenableFuture<T> submitListenable(@NotNull Callable<T> task) {
        showThreadPoolInfo("2. do submitListenable");
        return super.submitListenable(task);
    }

}
